﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Web.Support;
using Web.Library.DomainObject.SQLServer;
using Web.Library.Facade.SQLServer;
using System.Configuration;

public partial class SiteMaster : BaseMasterPage
{
    private SysGroupAuthorizationFacade FacadeAuth = new SysGroupAuthorizationFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
    private SysMenuFacade FacadeMenu = new SysMenuFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        string domain = HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.PathAndQuery, "/");//ConfigurationManager.AppSettings["DomainApp"].ToString();
        string AppDomainAppVirtualPath = System.Web.HttpRuntime.AppDomainAppVirtualPath.Replace("/","");
        string pathDomain = string.Empty;
        
        if(AppDomainAppVirtualPath!="")
            pathDomain = HttpContext.Current.Request.Url.AbsoluteUri.Replace(domain, "").Replace(AppDomainAppVirtualPath + "/", "").ToString().Split('?')[0];
        else
            pathDomain = HttpContext.Current.Request.Url.AbsoluteUri.Replace(domain, "").ToString().Split('?')[0];
         
        string feedback = IsAuthorized(pathDomain, Page.User.Identity.IsAuthenticated);
        switch (feedback)
        {
            case "loginUrl": Response.Redirect("~/Login.aspx", false); break;
            case "unauthorizedUrl": Response.Redirect("~/Error/Unauthorized.aspx", false); break;
        }
       
    }

    public string IsAuthorized(string pathDomain, bool isAuthenticated)
    {
        string feedback = string.Empty;
        //FB.DomainLayer.Menu menu = new FB.DomainLayer.Menu();
        //menu = new MenuFacade().RetrieveMenuByUrl(pathDomain);

        SysMenu menu = new SysMenu();
        menu = FacadeMenu.GetByUrl(pathDomain);

        if (menu != null)
        {
            if (menu.MenuID > 0)
            {
                if (!isAuthenticated)
                {
                    feedback = "loginUrl";
                }
                else
                {
                    //FB.DomainLayer.GroupAuthorization authorization = new FB.DomainLayer.GroupAuthorization();
                    //authorization = new GroupAuthorizationFacade().RetrieveAuthorizationByGroupIDAndMenuID(new FB.DomainLayer.AuthInfo().UserInfo().GroupID, menu.ID);

                    SysGroupAuthorization auth = new SysGroupAuthorization();
                    auth = FacadeAuth.RetrieveAuthorizationByGroupIDAndMenuID(base.GroupID, menu.MenuID);

                    if (auth != null)
                    {
                        if (auth.AuthID > 0)
                        {
                            // do something here <Do not remove this section>
                        }
                        else
                        {
                            feedback = "unauthorizedUrl";
                        }
                    }
                    else
                    {
                        feedback = "unauthorizedUrl";
                    }
                }
            }
        }
        return feedback;
    }
}
